package com.wtwd.device.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author zdl
 * @date 2021/11/5 13:21
 */
@Service
public class WXUserServiceImpl extends ThirdPartyUserServiceImpl {

    private static final Logger log = LoggerFactory.getLogger(WXUserServiceImpl.class);


    private static final String URL_GET_TOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token";
    private static final String URL_GET_USERINFO = "https://api.weixin.qq.com/sns/userinfo?access_token={1}&openid={2}";

    private static final String CLIENT_ID = "989741510409-31fscb4vkdq6oeie96890qte7ao72r1k.apps.googleusercontent.com";
    private static final String CLIENT_SECRET = "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";
    private static final String REDIRECT_URI = "http://localhost:9001/app/app-user/google-login";
    private static final String GRANT_TYPE = "authorization_code";
    @Resource
    private RestTemplate restSSLTemplate;

    @Override
    public ThirdPartyUserInfo getUserInfo(GetTokenResponseBase getTokenResponseBase) {
        GetTokenResponseDTO getTokenResponse = (GetTokenResponseDTO) getTokenResponseBase;
        JSONObject userInfo = restSSLTemplate.getForObject(URL_GET_USERINFO, JSONObject.class, getTokenResponse.getAccess_token(), getTokenResponse.getOpenid());
        ThirdPartyUserInfo thirdPartyUserInfo = new ThirdPartyUserInfo();
        thirdPartyUserInfo.setName(userInfo.getString("nickname"));
        thirdPartyUserInfo.setUserKey(getTokenResponse.getOpenid());
        return thirdPartyUserInfo;
    }

    @Override
    public String getUserKey(GetTokenResponseBase getTokenResponseBase) {
        return ((GetTokenResponseDTO) getTokenResponseBase).getOpenid();
    }

    @Override
    public GetTokenResponseBase getAccessToken(String code) {
        return  restSSLTemplate.postForObject(URL_GET_TOKEN, new GetTokenRequstDTO(code), GetTokenResponseDTO.class);
    }

    class GetTokenRequstDTO {
        private String code;
        private String appid;
        private String secret;
        private String grant_type;

        public GetTokenRequstDTO(String code) {
            this.code = code;
            this.appid = CLIENT_ID;
            this.secret = CLIENT_SECRET;
            this.grant_type = GRANT_TYPE;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getAppid() {
            return appid;
        }

        public void setAppid(String appid) {
            this.appid = appid;
        }

        public String getSecret() {
            return secret;
        }

        public void setSecret(String secret) {
            this.secret = secret;
        }

        public String getGrant_type() {
            return grant_type;
        }

        public void setGrant_type(String grant_type) {
            this.grant_type = grant_type;
        }
    }

    static class GetTokenResponseDTO extends GetTokenResponseBase {
        private String access_token;
        private String openid;
        private String unionid;
        private String expires_in;

        public GetTokenResponseDTO(String idToken) {
            super(idToken);
        }

        public String getAccess_token() {
            return access_token;
        }

        public void setAccess_token(String access_token) {
            this.access_token = access_token;
        }

        public String getOpenid() {
            return openid;
        }

        public void setOpenid(String openid) {
            this.openid = openid;
        }

        public String getUnionid() {
            return unionid;
        }

        public void setUnionid(String unionid) {
            this.unionid = unionid;
        }

        public String getExpires_in() {
            return expires_in;
        }

        public void setExpires_in(String expires_in) {
            this.expires_in = expires_in;
        }
    }
}
